package com.alex.algorithm.easy;

public class ReverseLinkedList {

    public static void main(String[] args) {
        LinkedListNode n1 = new LinkedListNode();
        n1.setVal(1);
        LinkedListNode n2 = new LinkedListNode();
        n1.setNext(n2);
        n2.setVal(2);
        LinkedListNode n22 = new LinkedListNode();
        n22.setVal(2);
        n2.setNext(n22);
        LinkedListNode n3 = new LinkedListNode();
        n3.setVal(3);
        n22.setNext(n3);

        reverseLinkedList(n1);

        while (n3.getNext() != null) {
            System.out.println(n3);
            n3 = n3.getNext();
        }
    }

    public static void reverseLinkedList(LinkedListNode node) {
        LinkedListNode prev = null;
        LinkedListNode cur = node;
        LinkedListNode next;
        while (cur != null) {
            next = cur.getNext();
            cur.setNext(prev);
            prev = cur;
            cur = next;
        }
    }
}
